package com.smfx.yxt.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.ExcelTemplate;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.excel.JxlsExcelView;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.LateCoachPlan;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.LateCoachQueryVO;
import com.smfx.yxt.service.ILateCoachService;
import com.smfx.yxt.view.LateCoachPlanView;
import com.smfx.yxt.view.LateCoachRegTotalView;

@RequestMapping("sys/lateCoach")
@Controller
public class SysLateCoachController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(SysLateCoachController.class);
	
	@Autowired
	private ILateCoachService lateCoachService;
	
	@RequestMapping(value = {"", "/"})
	public String Index() {
		return "sys/lateCoach";
	}
	
	@RequestMapping("json/pageList")
	@ResponseBody Object pageList(LateCoachQueryVO vo) {
		try {
			Page<LateCoachPlan> page = new Page<>();
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("WEEK"));
			sorts.add(new OrderBy("CLASS_ID"));
			sorts.add(new OrderBy("COACH_TYPE"));
			page.setSorts(sorts);
			Grid grid = new Grid();
			List<LateCoachPlanView> list = lateCoachService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---pageList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("json/updatePlan")
	@ResponseBody Object updatePlan(LateCoachPlan record) {
		try {
			lateCoachService.updateLateCoachPlan(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--updatePlan error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 辅导统计
	 * @return
	 */
	@RequestMapping("total")
	public String total() {
		return "sys/lateCoachTotal";
	}
	
	@RequestMapping("json/getRegTotalList")
	@ResponseBody Object getRegTotalList(LateCoachQueryVO vo) {
		return lateCoachService.selectRegTotalList(vo);
	}
	
	@RequestMapping("json/regTotal/import")
	ModelAndView regTotalImport(LateCoachQueryVO vo) {
		ModelAndView modelAndView = new ModelAndView();
		try {
			List<LateCoachRegTotalView> list = lateCoachService.selectRegTotalList(vo);
			JxlsExcelView view = new JxlsExcelView(ExcelTemplate.LATE_COACH_TOTAL);
			modelAndView.setView(view);
			modelAndView.getModelMap().put("startDate", vo.getStartDate());
			modelAndView.getModelMap().put("endDate", vo.getEndDate());
			modelAndView.getModelMap().put("list", list);
		} catch (Exception e) {
			logger.error("--regTotalImport, error={}", e);
		}
		return modelAndView;
	}
}
